package display.base 
{
	import controller.Controller;
	import display.DisplayApplicationBackground;
	import display.DisplayHighLight;
	import display.DisplayPlayer;
	import events.DisplayEvent;
	import events.UpdateEvent;
	import fl.motion.Color;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.LocalConnection;
	import flash.system.System;
	import flash.geom.ColorTransform;
	import utils.ColorWrap;
	import utils.transform.*;
	
	/**
	 * ...
	 * @author henrik
	 */
	public class ModuleBase extends MovieClip
	{   
		// shell
		private var shell:Shell;
		private var stageBg:DisplayApplicationBackground;
		
		// main container for the module AKA holder 
		
		public var holder:Sprite;
		
		// controller 
		
		public var control:Controller;
		
		public var bg:Sprite;
		public var bgHighLight:DisplayHighLight;
		
		// transform tool
		
		private var transformTool:TransformTool;
		
		// module proberties
		
		private var moduleName:String;
		private var moduleHeight:int;
		private var moduleWidth:int;
		private var moduleXpos:int;
		private var moduleYpos:int;
		
		// colors
		
		public var moduleBackGroundColor:uint = 0x000000;
		public var moduleAlpha:Number = 1;
		public var colorOne:uint = 0xcccccc;
		public var colorTwo:uint = 0xffffff;
		public var selectedColorOne:uint = 0x000000;
		public var selectedColorTwo:uint = 0xCCCCCC;
		public var mainColor:uint = 0xffff00;
		public var thumbSelectedColorOne:uint = 0x000000;
		public var invertedButtonColorOne:uint = 0xcccccc;
		public var invertedButtonColorTwo:uint = 0xffffff;
		public var invertedButtonFontColor:uint = 0x000000;
		public var playListItemColorOne:uint = 0xcccccc;
		public var playListItemColorTwo:uint = 0xffffff;
		public var playListItemIconColor:uint = 0x000000;
		public var thumbSelectedColorTwo:uint = 0xffffff;
		public var thumbColorOne:uint = 0x000000;
		public var thumbColorTwo:uint = 0xffffcc;
		public var ratingStarColor:uint = 0xFFFF00;
		public var fontColor:uint = 0x000000;
		public var activeFontColor:uint = 0x000000;
		
		
		public function ModuleBase(sh:Shell,mod:String,ctr:Controller,xpos:int,ypos:int,mWidth:int,mHeight:int,_bg:DisplayApplicationBackground) 
		{
			shell = sh;
			stageBg = _bg;
			
			moduleName = mod;
			moduleXpos = xpos;
			moduleYpos = ypos;
			moduleWidth = mWidth;
			moduleHeight = mHeight;
			
			control = ctr;
			
			control.addEventListener(DisplayEvent.COLOR_CHANGED, onColorchanged);
			control.addEventListener(DisplayEvent.ALPHA_CHANGED, onAlphaChanged);
			control.addEventListener(DisplayEvent.VISIBILITY_CHANGED, onVisibilityChanged);
			
			build();
		}
		
		private function onVisibilityChanged(e:DisplayEvent):void
		{
			if (e.data.item == moduleName)
			{
				this.visible = e.data.selected;
			}
		}
		
		private function onColorchanged(e:DisplayEvent):void
		{
			switch(e.data.itemCategory)
			{
				case 'moduleBackgroundColor':
				bg.transform.colorTransform = ColorWrap.changeColor(e.data.colorOne);
				bg.alpha = moduleAlpha;
				break;
				
				case 'mainColor':
				mainColor = e.data.colorOne;
				break;
				
				case 'thumbGradient':
				thumbColorOne = e.data.colorOne;
				thumbColorTwo = e.data.colorTwo;
				break;
				
				case 'thumbSelectedGradient':
				thumbSelectedColorOne = e.data.colorOne;
				thumbSelectedColorTwo = e.data.colorTwo;
				break;
				
				case 'ratingStarColor':
				ratingStarColor = e.data.colorOne;
				break;
				
				case 'activeFontColor':
				activeFontColor = e.data.colorOne;
				break;
				
				case 'fontColor':
				fontColor = e.data.colorOne;
				break;
				
				case 'invertedButtonFontColor':
				invertedButtonFontColor = e.data.colorOne;
				break;
				
				case 'invertedButtonGradient':
				invertedButtonColorOne = e.data.colorOne;
				invertedButtonColorTwo = e.data.colorTwo;
				break;
				
				case 'playlistItemExpandButtonGradient':
				playListItemColorOne = e.data.colorOne;
				playListItemColorTwo = e.data.colorTwo;
				break;
				
				case 'playlistItemExpandButtonIconColor':
				playListItemIconColor = e.data.colorOne;
				break;
			}
			
			update();
		}
		
		private function onAlphaChanged(e:DisplayEvent):void
		{
			switch(e.data.itemCategory)
			{
				case 'moduleBackgroundAlpha':
				moduleAlpha = e.data.alpha / 100;
				
				bg.alpha = moduleAlpha;
				break;
			}
		}
		
		private function build():void
		{
			transformTool = new TransformTool(new ControlSetStandard());
			transformTool.negativeScaling = false;
			transformTool.maxRotation = 0;
			
			holder = new Sprite;
			holder.x = moduleXpos;
			holder.y = moduleYpos;
			addChild(holder);
			
			bg = new Sprite;
			bg.graphics.beginFill(moduleBackGroundColor,moduleAlpha);
			bg.graphics.drawRect(0,0,moduleWidth,moduleHeight);
			bg.graphics.endFill();
			bg.name = 'bg';
			bg.buttonMode = true;
			bg.addEventListener(MouseEvent.MOUSE_DOWN, addTransform);
			stageBg.addEventListener(MouseEvent.CLICK, removeTransform);
			
			holder.addChild(bg);
			holder.addChild(transformTool);
		}
		
		private function removeTransform(e:MouseEvent):void
		{
			trace('remove transform');
			
			transformTool.target = null;
		}
		
		private function addTransform(e:MouseEvent):void
		{
			trace('add transform');
			
			transformTool.target = bg;
		}
		
		public function onMouseMove(e:MouseEvent):void 
		{
			
		};
		
		public function update():void { };
		
		public function clear():void
		{
			while (holder.numChildren != 0)
			{
				try
				{
					var s:DisplayObject = holder.getChildAt(0);
					
					holder.removeChild(s);
					
					s = null;
					
				}
				catch (e:Error)
				{
					break;
					trace('error: ' + e.message);
				}
			}
		}
	}
}